const path = require('path')
var Sequelize = require('sequelize');
const Model = Sequelize.Model;

var sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: path.join(__dirname, "database.sqlite")
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

// https://github.com/demopark/sequelize-docs-Zh-CN Sequelize 中文文档
const User = sequelize.define('user', {
  // 属性
  account: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false
  },
  introducer: {
    type: Sequelize.INTEGER,
    allowNull: false
  },
  code: {
    type: Sequelize.STRING,
    allowNull: false
  },
  code_use: {
    type: Sequelize.INTEGER,
    allowNull: false
  },
  status: {
    type: Sequelize.INTEGER,
    allowNull: false
  }
}, {
  // 参数
});

User.sync();
User.create({
  account: "rt",
  password: "rtx",
  introducer: 0,
  code: "Genesis",
  code_use: 1,
  status: 1
}).then((result)=> {
  console.log("New Database Create root User")
}, (err) => {
  console.log("Exist database User")
})

module.exports = {
  sequelize,
  User
}
